﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Reflection;
using Entities;
using DataAccess;

namespace Business
{
    public class GovOfficeRegionBusiness
    {
        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static GovOfficeRegionBusiness Instance { get; private set; }

        /// <summary>
        /// Initializes the <see cref="GovOfficeRegionBusiness"/> class.
        /// </summary>
        static GovOfficeRegionBusiness() { Instance = new GovOfficeRegionBusiness(); }

        /// <summary>
        /// Gets the government office region  by id.
        /// </summary>
        /// <param name="govOfficeRegionId">The gov office region id.</param>
        /// <returns></returns>
        public GovOfficeRegion GetGovOfficeRegionById(string govOfficeRegionId)
        {
            return GovOfficeRegionDataAccess.Instance.GetById(govOfficeRegionId);
        }

        /// <summary>
        /// Filters government office region by the specified keyword.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <param name="includeInactive">if set to <c>true</c> [include in active].</param>
        /// <returns></returns>
        public Collection<GovOfficeRegion> Filter(string keyword, bool includeInactive)
        {
            return GovOfficeRegionDataAccess.Instance.Filter(keyword, includeInactive);
        }

        /// <summary>
        /// Gets the government office region with 2 option include In-active or not
        /// </summary>
        /// <param name="includeInactive">if set to <c>true</c> [include in active].</param>
        /// <returns></returns>
        public Collection<GovOfficeRegion> GetGovOfficeRegion(bool includeInactive)
        {
            return GovOfficeRegionDataAccess.Instance.GetGovOfficeRegion(includeInactive);
        }


        /// <summary>
        /// parse List<T> to DataTable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items">The items.</param>
        /// <returns></returns>
        //public DataTable ToDataTable<T>(Collection<T> items)
        //{
        //    var dataTable = new DataTable(typeof(T).Name);
        //    //Get all the properties
        //    var Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        //    foreach (var prop in Props)
        //    {
        //        //Setting column names as Property names
        //        dataTable.Columns.Add(prop.Name);
        //    }
        //    foreach (T item in items)
        //    {
        //        var values = new object[Props.Length];
        //        for (var i = 0; i < Props.Length; i++)
        //        {
        //            //inserting property values to datatable rows
        //            values[i] = Props[i].GetValue(item, null);
        //        }
        //        dataTable.Rows.Add(values);
        //    }
        //    //put a breakpoint here and check datatable
        //    return dataTable;
        //}



        /// <summary>
        /// paging
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="listGov">The list gov.</param>
        /// <returns></returns>
        public Collection<GovOfficeRegion> Paging(int pageNumber, int pageSize, Collection<GovOfficeRegion> listGov)
        {
            int totalPage = listGov.Count % pageSize == 0 ? listGov.Count/pageSize : listGov.Count/pageSize + 1;
            if(pageNumber <= totalPage)
            {
                return new Collection<GovOfficeRegion>(listGov.Skip((pageNumber - 1)*pageSize).Take(pageSize).ToList());
            }
            return null;
        }
    }
}
